package com.platform.system.logic;


import com.alibaba.fastjson2.JSONArray;
import com.platform.common.utils.bean.BeanUtils;
import com.platform.system.domain.dto.device.HumanModelManagerDto;
import com.platform.system.domain.dto.device.SpeakerInfoDto;
import com.platform.system.domain.entity.device.HumanModelManager;
import com.platform.system.service.ISysSpeakerInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author Huangkai
 */
@Slf4j
@Component
public class HumanModelManagerLogic {

    @Resource
    ISysSpeakerInfoService sysSpeakerInfoService;

    public void selectSpeakerInfoListDetail(List<HumanModelManager> list, List<HumanModelManagerDto> humanModelManagerDtoList){
        Map<String, SpeakerInfoDto> stringSpeakerInfoDtoMap = null;
        List<String> uidList = list.stream().map(HumanModelManager::getSpeakerUid).collect(Collectors.toList());
        if(!CollectionUtils.isEmpty(uidList)){
            List<SpeakerInfoDto> speakerInfoDtoList = sysSpeakerInfoService.selectSpeakerInfoListByUidList(uidList);
            stringSpeakerInfoDtoMap = speakerInfoDtoList.stream().collect(Collectors.toMap(SpeakerInfoDto::getUid, Function.identity()));
        }
        Map<String, SpeakerInfoDto> finalStringSpeakerInfoDtoMap = stringSpeakerInfoDtoMap;
        list.forEach(humanModelManager -> {
            HumanModelManagerDto humanModelManagerDto = new HumanModelManagerDto();
            BeanUtils.copyProperties(humanModelManager, humanModelManagerDto);
            if(StringUtils.isNotEmpty(humanModelManager.getOpenQuestion())){
                List<HumanModelManagerDto.QuestionContent> questionContentList = JSONArray.parseArray(humanModelManager.getOpenQuestion(), HumanModelManagerDto.QuestionContent.class);
                humanModelManagerDto.setQuestionContentList(questionContentList);
            }
            if(StringUtils.isNotEmpty(humanModelManager.getSpeakerUid())){
                if(finalStringSpeakerInfoDtoMap != null && finalStringSpeakerInfoDtoMap.containsKey(humanModelManager.getSpeakerUid())){
                    humanModelManagerDto.setSpeakerInfoDto(finalStringSpeakerInfoDtoMap.get(humanModelManager.getSpeakerUid()));
                }
            }
            humanModelManagerDtoList.add(humanModelManagerDto);
        });
    }
}
